package com.airnewslinkosky.ticket.ui;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.VersionInfo;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.ui.client.ClientSearch;
import com.airnewslinkosky.ticket.ui.financial.FinancialSearch;
import com.airnewslinkosky.ticket.ui.flight.FlightQuery;
import com.airnewslinkosky.ticket.ui.login.Login;
import com.airnewslinkosky.ticket.ui.login.Logout;
import com.airnewslinkosky.ticket.ui.lowerlevel.LowerLevelQuery;
import com.airnewslinkosky.ticket.ui.order.OrderList;
import com.airnewslinkosky.ticket.util.Constants;
import com.airnewslinkosky.ticket.util.DateUtil;
import com.airnewslinkosky.ticket.util.NetIOUtils;
import com.mobclick.android.MobclickAgent;

/**
 * 主页面
 * @author 
 */
public class Main extends BaseActivity implements OnClickListener{
	private ProgressDialog pBar;
	private Handler handler = new Handler();
	public final String DownFileUrl = "http://m.jpfanli.com/b2b/";
	public final String DownFileName = "AirNewsLinkOSky.apk";
	/* 进度条与通知ui刷新的handler和msg常量 */
    private ProgressBar mProgress;
    private int progress;
    private static final int DOWN_UPDATE = 1;
    private static final int DOWN_OVER = 2;
    private boolean interceptFlag = false;
    private Dialog noticeDialog;
	private Dialog downloadDialog;
    
	private UpdateManager mUpdateManager;
	
	private StringBuffer sb = new StringBuffer(); 
	private SharedPreferences spLogin;
	private RelativeLayout isloginLayout;
	
	private TextView loginName;
	
	private String loginNameRe = "";
	private String isLoninOK = null;//是否登录
	private String autoLogin = null;//记着登录
	public final int reLoginCoed = 123321;
	public final int reLogoutCoed = 321123;
	public final int reFlightCoed = 421123;
	public final int reSystemSetCoed = 521123;
	public final int reOrderSearchCoed = 621123;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_second);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
        NetIOUtils.HttpTest(this);
        
		activityList.add(this); 
		
		spLogin = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
		
		loginName = (TextView)findViewById(R.id.loginName);
		
		isloginLayout = (RelativeLayout)findViewById(R.id.isloginLayout);
		isloginLayout.setVisibility(View.GONE);
		
		initConfig();
		
		findViewById(R.id.flight_view).setOnClickListener(this);
		findViewById(R.id.jiudian_view).setOnClickListener(this);
		findViewById(R.id.my_order_view).setOnClickListener(this);
		findViewById(R.id.login_view).setOnClickListener(this);
		findViewById(R.id.xiaji_view).setOnClickListener(this);
		findViewById(R.id.person_view).setOnClickListener(this);
		findViewById(R.id.financial_view).setOnClickListener(this);
		findViewById(R.id.setting_view).setOnClickListener(this);
		
//		SharedPreferences sp = getSharedPreferences(Constants.PREFS_FILE, 0);
//		String mobile = sp.getString(Constants.PHONE_NUM_KEY, null);
		//Toast.makeText(this, "欢迎您：" + mobile, Toast.LENGTH_LONG).show();
//		gridView = (GridView)findViewById(R.id.gridView);
//		gridAdapter = new GridAdapter(this, Constants.mainMenuList);
//		gridView.setAdapter(gridAdapter);
//		gridView.setOnItemClickListener(onItemClickListener);
		//检查新版本
		mUpdateManager = new UpdateManager(this);
        mUpdateManager.checkUpdateInfo();
		 
	}

    //初始化配置

    private void initConfig() {
        loginNameRe = Constants.USERNAME;
        isLoninOK = Constants.isLoninOKB2B;
        autoLogin = Constants.isAutoLoginB2B;
        System.out.println("Main autoLogin="+autoLogin);
        System.out.println("isLoninOK="+isLoninOK);
        System.out.println("loginNameRe="+loginNameRe);
        System.out.println("userIdB2B="+Constants.USERID);
        if(Constants.USERID != null && !Constants.USERID.equals("")) {
			System.out.println("onActivityResult LoginName"+loginNameRe);
			isloginLayout.setVisibility(View.VISIBLE);
			loginName.setText("欢迎您，" + loginNameRe + "           今天是：" + DateUtil.getDate() + " " + DateUtil.getWeek("yyyy-MM-dd",DateUtil.getDate()));
		} else {
			isloginLayout.setVisibility(View.GONE);
			isloginLayout.invalidate();
		}
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle b=data.getExtras();  
			loginNameRe = b.getString("loginNameB2B");
			isLoninOK = b.getString("isLoninOKB2B");
			autoLogin = b.getString("autoLoginB2B");
			System.out.println("onActivityResult isLoninOK"+isLoninOK);
			if(loginNameRe != null && loginNameRe.length() > 0) {
				System.out.println("onActivityResult LoginName"+loginNameRe);
				isloginLayout.setVisibility(View.VISIBLE);
				loginName.setText("欢迎您，" + loginNameRe + "           今天是：" + DateUtil.getDate() + " " + DateUtil.getWeek("yyyy-mm-dd",DateUtil.getDate()));
			} else {
				isloginLayout.setVisibility(View.GONE);
				isloginLayout.invalidate();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
    //订单是否登录
    private boolean isOrderLogin() {
    	 boolean isLogin = true;
	     if(Constants.USERID != null && !Constants.USERID.equals("")) {
			 Intent it = new Intent(Main.this, OrderList.class);
	    	 OrderList.searchOrder = null;
	    	 OrderList.orderist.clear();
			 startActivityForResult(it, reOrderSearchCoed);
			 overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	     } else {
	    	Intent it = new Intent(Main.this, Login.class);
	     	Login.isOrderList = "OK";
	     	Login.isFinacialList = null;
	     	isLogin = false;
			startActivity(it);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	     }
	     return isLogin;
    }
 
    //账务是否登录
    private boolean isFinacialLogin() {
    	 boolean isLogin = true;
	     if(Constants.USERID != null && !Constants.USERID.equals("")) {
			Intent it = new Intent(Main.this, FinancialSearch.class);
			startActivityForResult(it, reOrderSearchCoed);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	     } else {
	    	Intent it = new Intent(Main.this, Login.class);
	     	Login.isFinacialList = "OK";
	     	Login.isOrderList = null;
	     	isLogin = false;
			startActivity(it);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	     }
	     return isLogin;
    }
    
	
	@Override
	public void onBackPressed() {
		exitAppDialog(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
	  	case R.id.flight_view:
	  		Intent it = new Intent(Main.this, FlightQuery.class);
			startActivityForResult(it, reFlightCoed);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
			
	  		break;
	  	case R.id.jiudian_view:
	  		break;
	  	case R.id.my_order_view:
	  		isOrderLogin();
	  		break;
	  	case R.id.login_view:
	  		System.out.println("Constants.USERID =" + Constants.USERID );
	  		if(Constants.USERID != null && !Constants.USERID.equals("")) {
				Intent it1 = new Intent(Main.this, Logout.class);
				Logout.loginName = loginNameRe;
				startActivityForResult(it1, reLogoutCoed);
				overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
			} else {
				Intent it2 = new Intent(Main.this, Login.class);
				Login.flight = null;
				Login.isOrderList = null;
				startActivityForResult(it2, reLoginCoed);
				overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
			}
	  		break;
	  	case R.id.xiaji_view:
	  		Intent itQ = new Intent(Main.this, LowerLevelQuery.class);
			startActivity(itQ);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	  		break;
	  	case R.id.person_view:
	  		Intent itP = new Intent(Main.this, ClientSearch.class);
			startActivity(itP);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	  		break;
	  	case R.id.financial_view:
	  		isFinacialLogin();
	  		break;
	  	case R.id.setting_view:
	  		Intent itS = new Intent(Main.this, PersonCenter.class);
			startActivityForResult(itS, reSystemSetCoed);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	  		break;
		}
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}